﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Medianamik.Core;

namespace Medianamik.Core.ImportExport
{
    public class Medianamik2DataImporter : DataImporter<NodeType, NodeTypeProperty, NodeInstance, NodeProperty>
    {
        public Medianamik2DataImporter(DataSource<NodeType, NodeTypeProperty, NodeInstance, NodeProperty> dataSource)
            : base(dataSource)
        {

        }

        protected override void OnRecordTypesImported(int recordTypesCount, int importedTypesCount, bool canceled)
        {
            if (canceled)
                Logger.Trace(String.Format("L'importation des types a été annulée ({0}/{1}).", recordTypesCount,
                    importedTypesCount));
            else
                Logger.Trace(String.Format("Fin de l'importation des types ({0}/{1}).", recordTypesCount,
                    importedTypesCount));

            base.OnRecordTypesImported(recordTypesCount, importedTypesCount, canceled);
        }

        protected override ImportExportEventResult OnRecordTypeImporting(NodeType importedType, int importedTypeIndex, int importedTypesCount)
        {
            Logger.Trace(String.Format("Importation du type ({0}/{1}) : {2}.", importedTypeIndex + 1,
                        importedTypesCount, importedType.Name));

            return base.OnRecordTypeImporting(importedType, importedTypeIndex, importedTypesCount);
        }

        protected override bool OnRecordTypesImporting(IList<NodeType> importedTypes)
        {
            Logger.Trace("Début de l'importation des types.");

            return base.OnRecordTypesImporting(importedTypes);
        }

        protected override bool OnRecordTypePropertiesImporting(NodeType importedType, IList<NodeTypeProperty> importedProperties)
        {
            Logger.Trace("Début de l'importation des propriétés du type : " + importedType.Name);

            return base.OnRecordTypePropertiesImporting(importedType, importedProperties);
        }

        protected override ImportExportEventResult OnRecordTypePropertyImporting(NodeTypeProperty importedProperty,
            int importedPropertyIndex, int importedPropertiesCount)
        {
            Logger.Trace(String.Format("Importation de la propriété ({0}/{1}) : {2}.", importedPropertyIndex + 1,
                        importedPropertiesCount, importedProperty.Name));

            return base.OnRecordTypePropertyImporting(importedProperty, importedPropertyIndex, importedPropertiesCount);
        }

        protected override void OnRecordTypePropertyImported(NodeTypeProperty recordTypeProperty)
        {
            Logger.Trace("La propriété " + recordTypeProperty.Name + " a été importée.");

            base.OnRecordTypePropertyImported(recordTypeProperty);
        }

        protected override bool OnRecordTypeImportFailed(NodeType importedType, int importedTypeIndex, int importedTypesCount, string message)
        {
            string importedTypeName = GetImportedTypeName(importedType);

            if (String.IsNullOrEmpty(importedTypeName))
                Logger.Trace(String
                    .Format("Importation of type {0}/{1} has failed because of null or empty type name.",
                    importedTypeIndex, importedTypesCount));
            else
                Logger.Trace(String
                    .Format("Importation of type {0} ({1}/{2}) has failed. {3}",
                    importedTypeName, importedTypeIndex, importedTypesCount, message));

            return true;
        }

        protected override void OnRecordTypesImportFailed(string message)
        {
            Logger.Trace("L'importation a échouée. " + message);

            base.OnRecordTypesImportFailed(message);
        }

        protected override bool OnRecordTypePropertyImportFailed(NodeTypeProperty importedProperty,
            int importedPropertyIndex, int importedPropertiesCount, string message)
        {
            string importedTypePropertyName = GetImportedTypePropertyName(importedProperty);

            if (String.IsNullOrEmpty(importedTypePropertyName))
                Logger.Trace(String
                    .Format("Importation of type property {0}/{1} has failed because of null or empty type name.",
                    importedPropertyIndex, importedPropertiesCount));
            else
                Logger.Trace(String
                    .Format("Importation of type property {0} ({1}/{2}) has failed. {3}",
                    importedTypePropertyName, importedPropertyIndex, importedPropertiesCount, message));

            return true;
        }

        #region Abstract Members

        public override string GetImportedTypePropertyName(NodeTypeProperty property)
        {
            return property.Name;
        }
        public override string GetImportedTypeName(NodeType type)
        {
            return type.Name;
        }
        public override Guid? GetImportedTypePropertyId(NodeTypeProperty property)
        {
            return property.ID;
        }
        public override Guid? GetImportedTypeId(NodeType type)
        {
            return type.ID;
        }
        public override DataType GetImportedTypePropertyDataType(NodeTypeProperty property)
        {
            return property.DataType;
        }

        #endregion
    }
}
